#!/bin/bash

#Created by turk 2015-11-09
#Version 1.0 



#if [ $# -lt 2 ]; then
#	echo `date +"%Y-%m-%d %H:%M:%S"`  "Usage:<TABLE1> <TABLE2>"
#	exit 1
#fi

WORKPATH=/home/hadoop/turk
DATAPATH=/data/hadoop/101.227.160.27
JAVALIB=${WORKPATH}/lib/
LOGFILE=${WORKPATH}/log/apollo_data_demo.log


echo `date +"%Y-%m-%d %H:%M:%S"`  "-----------------------------" | tee -a $LOGFILE
echo `date +"%Y-%m-%d %H:%M:%S"`  "Shell Version 1.0 (2015-11-09)"     | tee -a $LOGFILE
echo `date +"%Y-%m-%d %H:%M:%S"`  "Apollo Data DEMO"  | tee -a $LOGFILE
echo `date +"%Y-%m-%d %H:%M:%S"`  "-----------------------------"  | tee -a $LOGFILE
echo `date +"%Y-%m-%d %H:%M:%S"`  "Start..."  | tee -a $LOGFILE


DATABUFF=${DATAPATH}/databuff


FILENUM=`ls ${DATAPATH}/databuff/*.txt | wc -l`

#random
function random()
{
    min=$1;
    max=$2-$1;
    num=$(date +%s+%N);
    ((retnum=num%max+min));
    echo $retnum;
}


if [ $FILENUM -lt 100 ]; then
	echo `date +"%Y-%m-%d %H:%M:%S"` "Start tar zxvf" | tee -a $LOGFILE
	cd ${DATAPATH}
	for tar in ${DATAPATH}/*.gz
	do 
		tar zxvf $tar -C ${DATAPATH}
		for txt in ${DATAPATH}/output/*.txt
		do 
			TIME=`date +"%Y%m%d%H%M%S"`
			mv $txt ${DATABUFF}/${TIME}_${txt##*/}
		done
	sleep 2
	done
	echo `date +"%Y-%m-%d %H:%M:%S"` "END tar zxvf" | tee -a $LOGFILE
fi

#mv num
out=$(random 2 50);

mv `ls ${DATABUFF}/*.txt | sort -u | head -${out}` ${DATAPATH}/flumebuff
echo `date +"%Y-%m-%d %H:%M:%S"` "mv ${out} files to flumebuff" | tee -a $LOGFILE
echo `date +"%Y-%m-%d %H:%M:%S"`      "Quit" | tee -a $LOGFILE

exit 0
